/*
https://leetcode.cn/problems/diameter-of-binary-tree/
543. 二叉树的直径
*/
/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:
    int diameterOfBinaryTree(TreeNode* root) {
        int max=0;
        maxDepth(root,max);
         return max;
    }
     int maxDepth(TreeNode* root,int& maxx) {
        if(root==NULL) return 0;
        int len1=maxDepth(root->left,maxx);
        int len2=maxDepth(root->right,maxx);
        maxx=max(maxx,len1+len2);
        return max(len1,len2)+1;
    }
};